const {MySql, DynamicQuery, PageResponse, switchSqlLog, TableConfig} = require('../index');
const {anyToEntity, anyToEntities} = require("../src/common");
switchSqlLog(true)


const log = (value) => {
    console.log(JSON.stringify(value, null, 2))
};

class UserInfo {
    id
    name
    age
    department_id
    department
}

(async () => {

    let mySql = new MySql({
        "host": "192.168.1.52",
        "port": 5306,
        "database": "test_zh",
        "user": "root",
        "password": "Jimmy007",
        connectionLimit: 10
    });

    await mySql.connect();
    const userConfig = new TableConfig("user_info", [
        "id",
        "name",
        "age",
        "json",
        "department_id"
    ]);
    userConfig.jsonColumn = ['json']
    userConfig.parents.push({
        parentObject: "department",
        parentTable: "department_info",
        parentIdName: "id",
        parentId: "department_id"
    })
    const dynamicQuery = new DynamicQuery(userConfig, mySql, {enableSqlLog: true});
    let r = await dynamicQuery.save({id: 2, name: 'zh1', age: 13, department_id: 1, json: ["zh"]});
    r = anyToEntity(r, () => new UserInfo());
    log(r)
    let r2 = await dynamicQuery.save({name: 'zh2', age: 13, json: ["zh3"]});
    log(r2)
    const all = await dynamicQuery.navigationFindAll({department: {name: "test1"}});
    console.log(anyToEntities(all, () => new UserInfo()))
    const pageResponse = new PageResponse();
    await dynamicQuery.find({}, pageResponse)
    log(pageResponse)
})()